<template>

  <div  class="m-container-small m-padded-tb-big animated fadeIn">
    <div class="ui container">
      <!--header-->
      <div class="ui top attached segment">
        <div class="ui middle aligned two column grid">
          <div class="column">
            <h3 class="ui teal header">分类</h3>
          </div>
          <div class="right aligned column">
            共 <h2 class="ui orange header m-inline-block m-text-thin"> {{types.length}} </h2> 个
          </div>
        </div>
      </div>
      <div class="ui attached segment m-padded-tb-large">
        <div class="ui labeled button m-margin-tb-tiny" v-for="type in types">


          <div v-if="type.id===$store.state.activeTypeId">
            <a @click="toType(type.id)" class="ui basic button teal" v-text="type.name"></a>
            <div class="ui basic left pointing label teal" v-text="type.count"></div>
          </div>
          <div v-else>
            <a @click="toType(type.id)" class="ui basic button" v-text="type.name"></a>
            <div class="ui basic left pointing label" v-text="type.count"></div>
          </div>

        </div>
      </div>

      <div class="ui top attached teal segment">
        <div class="ui padded vertical segment m-padded-tb-large" v-for="blog in blogs">
          <div class="ui middle aligned mobile reversed stackable grid" >
            <div class="eleven wide column">
              <h3 class="ui header" ><a style="cursor:pointer" @click="toBlog(blog.id)" target="_blank" class="m-black">{{blog.title}}</a></h3>
              <p class="m-text">{{blog.description}}</p>
              <div class="ui grid">
                <div class="eleven wide column">
                  <div class="ui mini horizontal link list">
                    <div class="item">
                      <img :src="blog.user.avatar" alt="" class="ui avatar image">
                      <div class="content"><a href="#" class="header">{{blog.user.nickname}}</a></div>
                    </div>
                    <div class="item">
                      <i class="calendar icon"></i><span>{{dateFormat(blog.updateTime)}}</span>
                    </div>
                    <div class="item">
                      <i class="eye icon"></i> <span>{{blog.views}}</span>
                    </div>
                  </div>
                </div>
                <div class="right aligned five wide column">
                  <a href="#" target="_blank" class="ui teal basic label m-padded-tiny m-text-thin">{{blog.type.name}}</a>
                </div>
              </div>
            </div>

            <div class="five wide column">
              <a style="cursor:pointer" @click="toBlog(blog.id)" target="_blank">
                <img :src="blog.firstPicture"  alt="" class="ui rounded image">
              </a>
            </div>

          </div>
        </div>
      </div>

      <!--footer-->
      <div class="ui bottom attached segment">
        <div class="ui middle aligned two column grid">
          <div class="column">

            <div v-if="preBoolean">
              <a style="cursor:pointer" @click="toPreType" class="ui mini teal basic button">上一页</a>
            </div>


          </div>
          <div class="right aligned column">

            <div v-if="nexBoolean">
              <a style="cursor:pointer" @click="toNexType" class="ui mini teal basic button">下一页</a>
            </div>
          </div>
        </div>
      </div>
    </div>
<!--    <div>-->
<!--      {{$store.state.types}}-->
<!--    </div>-->
<!--&lt;!&ndash;    <button @click="change">按钮</button>&ndash;&gt;-->
<!--&lt;!&ndash;    <div>{{ name }}</div>&ndash;&gt;-->

<!--    <h1>数量：{{$store.state.count}}</h1>-->
<!--    <button @click="changeEvent">添加数量</button>-->

<!--    <h1>总价：{{$store.getters.totalPrice}}</h1>-->
<!--    <div>{{student}}</div>-->
<!--    <div>{{mmm}}</div>-->
  </div>
</template>

<script>
import axios from "axios";

import UserBase from "@/views/UserBase";

export default {
  data(){
    return {
      types: [],
      activeTypeId: '',
      blogs: [],
      preBoolean: '',
      nexBoolean: '',
      curPage: '',
      n: '',
      show: true
    }
  },
  mounted() {
    // this.getStu();
    // this.getM();
    // this.showAxios();
    this.getType();
  },
  methods:{
    showV(){
      console.log(this.value);
      console.log(this.label);
    },
    generateClassName(id){
      if (id === $store.state.activeTagId){
        return 'ui basic left pointing large label m-margin-tb-tiny teal';
      }else {
        return 'ui basic left pointing large label m-margin-tb-tiny';
      }
    },
    toType(id){
      // alert(1212);
      this.$store.commit('setActiveTypeId',id);
      // this.$store.commit('setActiveTypeId',id);
      // location.reload();
      axios.get('http://localhost:7070/types/'+this.$store.state.activeTypeId).then((res)=>{
        console.log(res.data);
        this.types = res.data.types;
        this.blogs = res.data.page;
        this.preBoolean = res.data.preBoolean;
        this.nexBoolean = res.data.nexBoolean;
        this.curPage = res.data.curPage;
      });
    },
    toBlog(id){
      this.$store.commit('setBlogId',id);
      this.$router.push({path: '/blog'});
    },
    dateFormat(time) {
      var date=new Date(time);
      var year=date.getFullYear();
      /* 在日期格式中，月份是从0开始的，因此要加0
       * 使用三元表达式在小于10的前面加0，以达到格式统一  如 09:11:05
       * */
      var month= date.getMonth()+1<10 ? "0"+(date.getMonth()+1) : date.getMonth()+1;
      var day=date.getDate()<10 ? "0"+date.getDate() : date.getDate();
      var hours=date.getHours()<10 ? "0"+date.getHours() : date.getHours();
      var minutes=date.getMinutes()<10 ? "0"+date.getMinutes() : date.getMinutes();
      var seconds=date.getSeconds()<10 ? "0"+date.getSeconds() : date.getSeconds();
      // 拼接
      // return year+"-"+month+"-"+day+" "+hours+":"+minutes+":"+seconds;
      return year+"-"+month+"-"+day;
    },
    getType(){
      axios.get('http://localhost:7070/types/-1').then((res)=>{
        console.log(res.data);
        this.types = res.data.types;
        this.activeTypeId = res.data.activeTypeId;
        this.blogs = res.data.page;
        this.preBoolean = res.data.preBoolean;
        this.nexBoolean = res.data.nexBoolean;
        this.curPage = res.data.curPage;
      });
      this.$store.commit('setO2');
    },
    toPreType(){
      var url = 'http://localhost:7070/types/'+(this.activeTypeId)+"?page="+(this.curPage-1);
      // alert(url);
      axios.get(url).then((res)=>{
        console.log(res.data);
        this.types = res.data.types;
        this.activeTypeId = res.data.activeTypeId;
        this.blogs = res.data.page;
        this.preBoolean = res.data.preBoolean;
        this.nexBoolean = res.data.nexBoolean;
        this.curPage = this.curPage-1;
      });
    },
    toNexType(){
      var url = 'http://localhost:7070/types/'+(this.activeTypeId)+"?page="+(this.curPage+1);
      // alert(url);
      axios.get(url).then((res)=>{
        console.log(res.data);
        this.types = res.data.types;
        this.activeTypeId = res.data.activeTypeId;
        this.blogs = res.data.page;
        this.preBoolean = res.data.preBoolean;
        this.nexBoolean = res.data.nexBoolean;
        this.curPage = this.curPage+1;
      });
    }
  }
}
</script>

<style scoped>


</style>